
<!-- TODO 改stu为homework-->
<!-- TODO 5改Stu为Homework -->
<template>
  <div>
    <!-- 搜索栏 -->
    <el-card id="search">
      <el-row>
        <!-- <el-col :span="22">
          <el-input v-model="searchModal.name" placeholder="作业名" clearable></el-input>
          <el-button @click="getHomeworkList" type="primary" icon="el-icon-search" round>查询</el-button>
        </el-col> -->
        <!-- 新增按钮 -->
        <el-col :span="2" align="right">
          <el-button @click="dialogAddVisible = true" type="primary" icon="el-icon-plus" circle
            v-if="isAuthor"></el-button>
        </el-col>
      </el-row>
    </el-card>
    <!-- 结果列表 -->
    <!-- TODO 4 改表格 -->
    <el-card id="list">
      <el-table :data="homeworkList" stripe style="width: 100%">
        <el-table-column prop="date" align="center" label="#" width="80">
          <!-- (pageNo-1)*pageSize+index+1 -->
          <template slot-scope="scope">
            {{ (searchModal.pageNo - 1) * searchModal.pageSize + scope.$index + 1 }}
          </template>
        </el-table-column>
        <el-table-column prop="name" label="作业名" align="center">
        </el-table-column>
        <el-table-column prop="createTime" label="创建时间" align="center">
        </el-table-column>
        <el-table-column prop="expireTime" label="到期时间" align="center">
        </el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <span :class="{ 'committed': scope.row.committed, 'not-committed': !scope.row.committed }">
              {{ scope.row.committed ? '已提交' : '未提交' }}
            </span>
          </template>
        </el-table-column>

        <el-table-column fixed="right" prop="operator" label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button type="primary" @click="display(scope.row)" plain>查看</el-button>
            <!-- <el-button type="danger" @click="deleteHomework(scope.row)" plain>删除</el-button> -->
          </template>
        </el-table-column>

      </el-table>
    </el-card>
    <!-- 分页组件 -->
    <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange"
      :current-page="searchModal.pageNo" :page-sizes="[5, 10, 50, 100]" :page-size="searchModal.pageSize"
      layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>

    <!-- 新增的对话框 -->
    <!-- 新增表单 -->
    <!-- TODO 3 该表单 -->
    <el-dialog @close='clearForm' id="add" :title="title" :visible.sync="dialogAddVisible">
      <!-- <el-form :model="homeworkModal" ref='homeworkFromRef' :rules="rules"> -->
      <el-form :model="homeworkModal" ref='homeworkFromRef'>
        <el-form-item label="作业名" prop="name" :label-width="formLabelWidth">
          <el-input v-model="homeworkModal.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="截止日期" :label-width="formLabelWidth">
          <el-date-picker v-model="homeworkModal.expireTime" type="datetime" placeholder="选择日期时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否开启自动更名" prop="cTno" :label-width="formLabelWidth">
          <el-input v-model="homeworkModal.autoname" autocomplete="off" placeholder="0否 1是"></el-input>
        </el-form-item>
        <el-form-item label="自动更名格式" :label-width="formLabelWidth">
          <el-input v-model="homeworkModal.autonameFormat" autocomplete="off"
            placeholder="<class>_<id>_<name>_计组作业"></el-input>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addHomework()">确 定</el-button>
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <!-- <el-button v-if="this.title == '新增用户'" type="primary" @click="saveHomework('0')">确 定</el-button>
        <el-button v-if="this.title == '修改用户'" type="primary" @click="saveHomework('1')">确 定</el-button> -->
      </div>
    </el-dialog>
  </div>
</template>

<script>
import homeworkApi from '@/api/homework'
import studentApi from '@/api/stu'
import classApi from '@/api/class'
import { getStudentId, getToken, setStudentId } from '@/utils/auth';
import { mapGetters } from 'vuex'
// import { getList } from '@/api/table';

export default {
  userId: "",
  computed: {
    ...mapGetters([
      'userId'
    ])
  },
  data() {
    return {
      total: 0,
      searchModal: {
        pageNo: 1,
        pageSize: 10
      },
      // TODO2 改类
      homeworkModal: {
        id: "",
        name: "",
        createTime: "",
        expireTime: "",
        autoname: "",
        autonameFormat: ""
      },
      homeworkList: [],
      dialogAddVisible: false,
      formLabelWidth: "130px",
      formLabelWidth2: "300px",
      title: "",
      isAuthor: false
      // rules: {
      //   // TODO 改限制
      //   name: [
      //     { required: true, message: '请输入作业名', trigger: 'blur' },
      //     { min: 3, max: 15, message: '长度在 3 到 15 个字符', trigger: 'blur' }
      //   ],
      //   expireTime: [
      //     { required: true, message: '请输入截止日期', trigger: 'blur' }
      //   ]
      // }
    }
  },
  methods: {

    editHomework(homework, index) {
      this.homeworkModal = homework;
      this.homeworkList[index] = JSON.parse(JSON.stringify(homework))
      this.openForm('修改用户');
    },
    deleteHomework(homework) {
      this.$confirm('此操作将永久删除 ' + homework.name + ' , 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        homeworkApi.deleteHomework(homework.cno).then(response => {
          this.$message({
            type: 'success',
            message: response.message
          });
          this.getHomeworkList();
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    addHomework() {
      this.$refs.homeworkFromRef.validate((valid) => {
        if (valid) {
          //提交验证
          const token = getToken();
          homeworkApi.addHomework(this.homeworkModal).then(response => {
            //成功提示  
            if (response.code == 20000) {
              this.$message({
                message: response.message,
                type: 'success'
              });
              //关闭对话框
              this.dialogAddVisible = false;
              //刷新表格
              this.getHomeworkList();
            }
            else {
              this.$message.error(
                response.message
              );
            }
          });
        }
        else {
          console.log("error submit");
          return false;
        }
      });
    },
    display(homework) {
      this.$router.push({ name: "HomeworkDetail", params: { id: homework.id } });
    },
    handleSizeChange(pageSize) {
      this.searchModal.pageSize = pageSize;
      this.getHomeworkList();
    },
    handleCurrentChange(pageNo) {
      this.searchModal.pageNo = pageNo;
      this.getHomeworkList();
    },
    getHomeworkList() {
      homeworkApi.getHomeworkList(this.searchModal).then(response => {
        this.homeworkList = response.data;
        this.total = response.data.total;
      });
    },
    getStudentInfo() {
      studentApi.getStudentByUserId(this.userId).then(response => {
        if (response.code == 20000) {
          setStudentId(response.data.id);
          this.myname = response.data.name;
        }
      })
    },
    judgeAuthor() {
      classApi.getAuthor(getStudentId()).then(response => {
        if (response.code == 20000) {
          if (response.data.studentId == getStudentId()) {
            this.isAuthor = true;
          }
        }
      })
    },
    openForm(title) {
      this.dialogAddVisible = true;
      this.title = title;
    },
    clearForm() {
      this.homeworkModal = {};
      this.$refs.homeworkFromRef.clearValidate();
    }
  },
  // 钩子函数，加载的时候就执行
  created() {
    this.getStudentInfo();
    this.searchModal.studentId = getStudentId();
    this.getHomeworkList();
    this.judgeAuthor();
  }
};
</script>

<style>
#search .el-input {
  width: 200px;
  margin-right: 10px;
}

.el-dialog .el-input {
  width: 100%;
}

.committed {
  background: linear-gradient(45deg, #00cc00, #006600);
  border-radius: 5px;
  color: white;
  padding: 5px 10px;
  display: inline-block;
}

/* 未提交的样式 */
.not-committed {
  background: linear-gradient(45deg, #ff0000, #990000);
  border-radius: 5px;
  color: white;
  padding: 5px 10px;
  display: inline-block;
}

/* .el-input {
  width: 200px;
  margin-right: 10px;
} */
</style>